package com.xsoft.navigation.activity;

import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;

import com.baidu.mapapi.walknavi.WalkNavigateHelper;
import com.baidu.mapapi.walknavi.adapter.IWNaviStatusListener;
import com.baidu.mapapi.walknavi.adapter.IWRouteGuidanceListener;
import com.baidu.mapapi.walknavi.model.RouteGuideKind;
import com.baidu.platform.comapi.walknavi.WalkNaviModeSwitchListener;
import com.baidu.platform.comapi.walknavi.widget.ArCameraView;

import androidx.annotation.Keep;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

/**
 * created by zhangkai on 2021/7/7
 * email: 405547628@qq.com
 * desc:
 */
@Keep
public class WNaviGuideActivity extends AppCompatActivity {
    private static final String TAG = WNaviGuideActivity.class.getSimpleName();
    private WalkNavigateHelper navigateHelper;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
                | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        navigateHelper = WalkNavigateHelper.getInstance();
        View view = navigateHelper.onCreate(this);
        if (view != null) {
            setContentView(view);
        }
        navigateHelper.setWalkNaviStatusListener(new IWNaviStatusListener() {
            @Override
            public void onWalkNaviModeChange(int mode, WalkNaviModeSwitchListener walkNaviModeSwitchListener) {
                Log.i(TAG, "onWalkNaviModeChange mode = " + mode);
                navigateHelper.switchWalkNaviMode(WNaviGuideActivity.this, mode, walkNaviModeSwitchListener);
            }

            @Override
            public void onNaviExit() {
                Log.i(TAG, "onNaviExit");
            }
        });
        boolean result = navigateHelper.startWalkNavi(this);
        Log.i(TAG, "result = " + result);
        //通过IWRouteGuidanceListener接口各种回调方法开发者可以对导航状态进行相应处理，主要包括导航开始、结束，导航过程中偏航、
        // 偏航结束，诱导信息（诱导图标、诱导信息、诱导类型、剩余时间、剩余距离、震动等）
        navigateHelper.setRouteGuidanceListener(this, new IWRouteGuidanceListener() {
            //诱导图标更新
            @Override
            public void onRouteGuideIconUpdate(Drawable drawable) {
                Log.i(TAG, "onRouteGuideIconUpdate");
            }

            //诱导类型枚举
            @Override
            public void onRouteGuideKind(RouteGuideKind routeGuideKind) {
                Log.i(TAG, "onRouteGuideKind");
            }

            /**
             * 诱导信息
             * @param charSequence 第一行显示的信息，如“沿当前道路”
             * @param charSequence1  第二行显示的信息，比如“向东出发”，第二行信息也可能为空
             */
            @Override
            public void onRoadGuideTextUpdate(CharSequence charSequence, CharSequence charSequence1) {
                Log.i(TAG, "onRoadGuideTextUpdate " + charSequence + " " + charSequence1);
            }

            //总的剩余距离
            @Override
            public void onRemainDistanceUpdate(CharSequence charSequence) {
                Log.i(TAG, "onRemainDistanceUpdate " + charSequence);
            }

            //总的剩余时间
            @Override
            public void onRemainTimeUpdate(CharSequence charSequence) {
                Log.i(TAG, "onRemainTimeUpdate " + charSequence);
            }

            //GPS状态发生变化，来自诱导引擎的消息
            @Override
            public void onGpsStatusChange(CharSequence charSequence, Drawable drawable) {
                Log.i(TAG, "onGpsStatusChange " + charSequence);
            }

            //已经开始偏航
            @Override
            public void onRouteFarAway(CharSequence charSequence, Drawable drawable) {
                Log.i(TAG, "onRouteFarAway " + charSequence);
            }

            //偏航规划中
            @Override
            public void onRoutePlanYawing(CharSequence charSequence, Drawable drawable) {
                Log.i(TAG, "onRoutePlanYawing " + charSequence);
            }

            //重新算路成功
            @Override
            public void onReRouteComplete() {
                Log.i(TAG, "onReRouteComplete");
            }

            //抵达目的地
            @Override
            public void onArriveDest() {
                Log.i(TAG, "onArriveDest");
            }

            @Override
            public void onIndoorEnd(Message message) {
                Log.i(TAG, "onIndoorEnd");
            }

            @Override
            public void onFinalEnd(Message message) {
                Log.i(TAG, "onFinalEnd");
            }

            //震动
            @Override
            public void onVibrate() {
                Log.i(TAG, "onVibrate");
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        navigateHelper.resume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        navigateHelper.pause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        navigateHelper.quit();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == ArCameraView.WALK_AR_PERMISSION) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_DENIED) {
                Toast.makeText(WNaviGuideActivity.this, "没有相机权限,请打开后重试", Toast.LENGTH_SHORT).show();
            } else if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                navigateHelper.startCameraAndSetMapView(WNaviGuideActivity.this);
            }
        }
    }
}
